package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolCommunity.entity.Commodity;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.mapper.CommodityMapper;
import com.schoolCommunity.service.CommodityService;
import com.schoolCommunity.service.CommodityTransactionService;
import com.schoolCommunity.util.InsertES;
import com.schoolCommunity.util.UploadCommodityPicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-10-10
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {

    @Resource
    private CommodityMapper commodityMapper;
    @Autowired
    private UploadCommodityPicture uploadCommodityPicture;
    @Autowired
    private InsertES insertES;
    @Autowired
    private CommodityTransactionService commodityTransactionService;

    //添加一个商品
    @Override
    public Information InsertCommodity(Commodity commodity) throws IOException {
        Information information = new Information();

        int insert = commodityMapper.insert(commodity);
        if (insert > 0) {

            //        获取插入的id
            long lastId = commodityMapper.getLastId();
            //获取数据库的一些默认值
//        获取不需要？
            commodity = commodityMapper.selectById(lastId);
            //放入ES
            insertES.InsertForIndex(commodity);

            information.setCode(200).setMessage("商品添加成功，等待审核");
        } else {
            information.setCode(404).setMessage("商品添加失败，请稍后重试");
        }

        return information;
    }

    //添加商品图片
    @Override
    public Information UpdateCommodityPicture(MultipartFile file, long CommodityId) throws IOException {
        Information information = new Information();
        Commodity commodity = commodityMapper.selectById(CommodityId);
        if (!commodity.getPictureUrl().equals("default.jpg")) {
            File f2 = new File("E:/项目/学校社区/schoolCommunityVue/src/schoolCommunityPicture/commodityPicture/"+commodity.getPictureUrl());
            //先删除原来的图片
            f2.delete();

            File f3 = new File("E:/项目/学校社区/vue-admin-template/src/views/schoolCommunityPicture/commodityPicture/"+commodity.getPictureUrl());
            //先删除原来的图片
            f3.delete();

        }
        //返回图片存储路径
        String pictureURL = uploadCommodityPicture.upload(file, commodity.getName());


        commodity.setPictureUrl(pictureURL);
        int i = commodityMapper.updateById(commodity);
        if (i > 0) {
            insertES.UpdateCommodityPicture(pictureURL, CommodityId);
            information.setCode(200).setMessage("图片写入成功");
        } else {
            information.setCode(404).setMessage("图片写入失败");
        }
        return information;
    }

    //用户主动下架商品
    @Override
    public Information soldOutCommodity(long CommodityId) throws IOException {
        Information information = new Information();


        QueryWrapper queryWrapper = new QueryWrapper();
        //筛选出没有被购买的商品
        queryWrapper.eq("betake", 0);
        queryWrapper.eq("id", CommodityId);

        //修改上架状态为1
        int i = commodityMapper.update(new Commodity().setIsOn(1), queryWrapper);
        if (i > 0) {
            //ES同步修改下架
            insertES.UpdateCommodity(new Commodity().setId(CommodityId).setIsOn(1));
            information.setMessage("商品已下架").setCode(200);
        } else {
            information.setMessage("下架失败,商品或已被购买。").setCode(404);
        }


        return information;
    }

    //获取某个用户的商品
    @Override
    public Information GetMyCommodity(long memberId) {
        Information information = new Information();

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("member_id", memberId);

        List list = commodityMapper.selectList(queryWrapper);
        if (list.isEmpty()) {
            information.setCode(200).setMessage("暂无商品信息");
        } else {
            information.setMessage("商品查找成功").setCode(200).setData(list);
        }

        return information;
    }

    //分页获取数据
    @Override
    public Information GetCommodityPage(int PageNo) {
        Information information = new Information();
        //分页获取数据
        Page<Commodity> page = new Page<>(PageNo, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("betake", 0);
        queryWrapper.eq("suggest", 1);
        queryWrapper.eq("isOn", 0);
        IPage<Commodity> commodityIPage = commodityMapper.selectPage(page, queryWrapper);
        List<Commodity> records = commodityIPage.getRecords();
        if (!records.isEmpty()) {  //不为空，有数据
            information.setData(records).setCode(200).setMessage("上架商品获取成功");
        } else {
            information.setCode(404).setMessage("暂无商品数据");
        }
        return information;
    }

    //获取最大分页书
    @Override
    public long GetMaxPageNo() {
        Page<Commodity> page = new Page<>(1, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("betake", 0);
        queryWrapper.eq("suggest", 1);
        queryWrapper.eq("isOn", 0);
        IPage<Commodity> commodityIPage = commodityMapper.selectPage(page, queryWrapper);
        return commodityIPage.getPages();
    }

    //用户购买商品
    @Override
    public Information BuyCommodity(long BuyMemberId, long CommodityId) {
        Information information = new Information();
        //商品改为已经出售并且下架
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", CommodityId);
        //获取商品信息
        Commodity commodity = commodityMapper.selectOne(queryWrapper);
        //设置为已经被购买并下架
        commodity.setIsOn(1).setBetake(1);
        //提交修改
        int i = commodityMapper.updateById(commodity);
        commodityTransactionService.InsertCommodityTransaction(BuyMemberId, commodity);

        //返回商品信息，懒得弄了，前端输出卖家信息
        information.setMessage("购买成功").setCode(200).setData(commodity);
        return information;
    }

    //根据商品id删除商品
    @Override
    public Information DeleteCommodity(long commodityId) {
        Information information = new Information();
        int i = commodityMapper.deleteById(commodityId);
        if (i > 0) {
            information.setMessage("商品删除成功").setCode(200);
        } else {
            information.setMessage("商品删除失败").setCode(404);
        }
        return information;
    }

    @Override
    public Information SuggestCommodity(long commodityId, int suggestResult) {
        Information information = new Information();
        Commodity commodity = new Commodity();
        commodity.setId(commodityId).setSuggest(suggestResult);
        int i = commodityMapper.updateById(commodity);
        if (i > 0) {
            information.setCode(200).setMessage("商品修改成功");
        } else {
            information.setCode(404).setMessage("商品修改失败");
        }
        return information;
    }

    @Override
    public Information GetNoSuggestCommodity(long pageNo) {
        Information information = new Information();
        Page<Commodity> page = new Page<>(pageNo, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("suggest", 0);
        IPage iPage = commodityMapper.selectPage(page, queryWrapper);
        List records = iPage.getRecords();
        if (records.isEmpty()) {
            information.setMessage("没有数据").setCode(404);
        } else {
            information.setMessage("数据获取成功").setCode(200).setData(records);
        }
        return information;
    }

    @Override
    public long GetNoSuggestCommodityMaxPage() {
        Information information = new Information();
        Page<Commodity> page = new Page<>(1, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("suggest", 0);
        IPage iPage = commodityMapper.selectPage(page, queryWrapper);
        return iPage.getPages();
    }

    @Override
    public Information GetAllNoOnCommodity(long pageNo) {
        Information information = new Information();
        Page<Commodity> page = new Page<>(pageNo, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("isOn", 1);
        IPage iPage = commodityMapper.selectPage(page, queryWrapper);
        List records = iPage.getRecords();
        if (records.isEmpty()) {
            information.setMessage("没有数据").setCode(404);
        } else {
            information.setMessage("数据获取成功").setCode(200).setData(records);
        }
        return information;

    }

    @Override
    public long GetAllNoOnCommodityMaxPage() {
        Page<Commodity> page = new Page<>(1, 20);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("isOn", 1);
        IPage iPage = commodityMapper.selectPage(page, queryWrapper);
        return iPage.getPages();
    }

    @Override
    public Information PutOnCommodity(long CommodityId) {
        Information information = new Information();
        Commodity commodity = new Commodity();
        commodity.setId(CommodityId).setIsOn(0);
        int i = commodityMapper.updateById(commodity);
        if(i>0){
            information.setMessage("上架成功").setCode(200);
        }else{
            information.setMessage("上架失败").setCode(404);
        }
        return information;
    }


}
